package com.github.user.controller.app;


import cn.hutool.core.util.StrUtil;
import com.github.api.auth.bo.UserInfoInTokenBO;
import com.github.api.auth.constant.SysTypeEnum;
import com.github.api.auth.feign.AccountFeignClient;
import com.github.api.auth.vo.TokenInfoVO;
import com.github.common.core.response.ServerResponseEntity;
import com.github.user.dto.UserRegisterDTO;
import com.github.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户信息
 */
@RestController
@RequestMapping("/ua/user/register")
@Tag(name = "app-用户注册接口")
public class UserRegisterController {

    @Resource
    private UserService userService;

    @Resource
    private AccountFeignClient accountFeignClient;

    @Operation(summary = "用户注册")
    @PostMapping
    public ServerResponseEntity<TokenInfoVO> register(@Valid @RequestBody UserRegisterDTO param){

        if (StrUtil.isBlank(param.getNickName())){
            // 默认使用用户名
            param.setNickName(param.getUserName());
        }

        //1.保存账户信息（统一账户信息以及用户信息）
        Long uid = userService.save(param);
        //2.登录--调用授权服务的登录，让前端拿到准入token
        UserInfoInTokenBO userInfoInTokenBO = new UserInfoInTokenBO();
        userInfoInTokenBO.setUid(uid);
        userInfoInTokenBO.setUserId(param.getUserId());
        //这里是app登录，所以是普通用户系统
        userInfoInTokenBO.setSysType(SysTypeEnum.ORDINARY.value());
        userInfoInTokenBO.setIsAdmin(0);
        return accountFeignClient.storeTokenAndGetVo(userInfoInTokenBO);


    }



























}
